package com.link.base.base.signin.controller;

import com.link.base.base.signin.model.SignIn;
import com.link.base.base.signin.service.SignInService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 签到
 */
@Controller
@RequestMapping("/link/signIn")
public class SignInController extends BasicController<SignIn> {


    @Resource
    private SignInService signInService;

    @Override
    public BasicService<SignIn> getBasicService() throws Exception {
        return signInService;
    }

    /**
     * 当前登录用户签到
     */
    @RequestMapping(value = "/currentUserSign")
    @ResponseBody
    public Map<String, Object> currentUserSign(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            Long userId = user.getId();
            signInService.currentUserSign(userId);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 校验能否签到
     */
    @RequestMapping(value = "/ableSignIn")
    @ResponseBody
    public Map<String, Object> ableSignIn(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            Long userId = user.getId();
            //能否签到
            boolean ableSignInFlag = signInService.ableSignIn(userId);
            result.put("ableSignIn", ableSignInFlag);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("ableSignIn", false);
        }
        return result;
    }
}
